<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
defined ('IN_AGORA') or die;

class TaskController extends AgoraController {

	function __construct() {
		parent::__construct();
		if (!$this->agora_user['id']) {
			Agora::showError('Access denied');
			Agora::redirect(Agora::getRefferer());
		}

		$this->loadDefaultView();
		$this->upload_model = & Model::getInstance('UploadModel');
	}

	function _default() {
		$uploads = $this->upload_model->loadUser($this->agora_user['id'],$this->agora_config['o_uploadile_laws']);
		$this->view->assignRef('uploads',$uploads);
		
		$sortdesc = Agora::getVar('desc');
		$sortdesc = (int)$sortdesc == 1 ? 0 : 1;
		$this->view->assignRef('sortdesc',$sortdesc);
		
		$style = $this->agora_config['o_default_style'];
		$this->view->assign('style',$style);
		
		$this->view->template = 'upload';
	}

	function userdelete() {
		$id = Agora::getVar('id');
		$user_id = Agora::getVar('user_id');
		$user_id = (int) $user_id;
		if ($this->agora_user['is_superadmin'] || ((int)$this->agora_config['o_allow_delete_attach'] && (int)$user_id == (int)$this->agora_user['id'])) {
			$this->upload_model->delete((int)$user_id, $id, $this->agora_config['o_uploadile_laws']);
		}
		Agora::redirect(Agora::getRefferer());
	}

	function delete() {
		$id = Agora::getVar('id');
		if ((int)$this->agora_config['o_allow_delete_attach']) {
			$this->upload_model->delete($this->agora_user['id'], $id, $this->agora_config['o_uploadile_laws']);
		}
		$this->redirect('type','!page','!action');
	}

	function add() {
	
		static $errors = array(
			-1 => 'File format not allowed',
			-2 => 'File type not allowed',
			-3 => 'Mime type is undefined',
			-4 => 'File has forbidden mime type',
			-5 => 'Cannot create upload dir',
			-6 => 'Cannot move uploaded file',
		);
		
		$redir = Agora::getVar('redir');
		
		if($redir == 'self') {
			$id_hidden = (int)Agora::getPostVar('id_hidden', '');
			$msg_hidden = Agora::getPostVar('msg_hidden', '');
			$subject_hidden = Agora::getPostVar('subject_hidden', '');
			$desc_hidden = Agora::getPostVar('desc_hidden', '');
			$userid_hidden = Agora::getPostVar('userid_hidden', '');
			$username_hidden = Agora::getPostVar('username_hidden', '');
			$smilies_hidden = Agora::getPostVar('smilies_hidden', '');
			$ticon_hidden = Agora::getPostVar('ticon_hidden', '');
			if (isset($_SESSION['form_hidden'])) {
				unset($_SESSION['form_hidden']);
			}
			$_SESSION['form_hidden'] = array(
				'message' => $msg_hidden,
				'subject' => $subject_hidden,
				'desc' => $desc_hidden,
				'userid' => $userid_hidden,
				'username' => $username_hidden,
				'smilies' => $smilies_hidden,
				'ticon' => $ticon_hidden,
				'id' => $id_hidden,
			);
		}
		
		if (!isset($_FILES['upload']) || $_FILES['upload']['error'] != 0) {
			Agora::showError('You have not selected a file to upload');
			
			if ($redir == 'self') {
				Agora::redirect(Agora::getRefferer());
			} else {
				$this->redirect('type');
			}
		};

		$upload = $_FILES['upload'];

		$user_model = & Model::getInstance('UserModel');
		$moderator = $user_model->moderateAny($this->agora_user['id']);
		
		if ($moderator) {
			$maxsize = $this->agora_config['o_uploadile_maxsize_modo'];
			$limit = $this->agora_config['o_uploadile_limit_modo'];
		} else {
			$maxsize = $this->agora_config['o_uploadile_maxsize_member'];
			$limit = $this->agora_config['o_uploadile_limit_member'];
		}
		
		if((int)$maxsize > 0 && $upload['size'] > ($maxsize*1024)) {
			Agora::showError(Agora::lang('Upload file too big'));
			
			if ($redir == 'self') {
				Agora::redirect(Agora::getRefferer());
			} else {
				$this->redirect('type');
			}
		}
		
		$current_space = $this->upload_model->getUserSpace($this->agora_user['id']);
		
		if((int)$limit > 0 && ($upload['size'] + $current_space) > ($limit*1024)) {
			Agora::showError('Upload limit exceeded');
			if ($redir == 'self') {
				Agora::redirect(Agora::getRefferer());
			} else {
				$this->redirect('type');
			}
		}
		
		$upload_name = Agora::getPostVar('upload_name');

		$file_name = $this->upload_model->add($this->agora_user['id'],$upload,$upload_name,$this->agora_config['o_uploadile_laws']);

		if (is_int($file_name)) {
			Agora::showError(Agora::lang('Upload error') . ': ' . Agora::lang($errors[$file_name]));
			if ($redir == 'self') {
				Agora::redirect(Agora::getRefferer());
			} else {
				$this->redirect('type');
			}
		}

		//if ($this->agora_config['o_uploadile_thumb'] == '1') {
			$this->upload_model->makeThumb($this->agora_user['id'], $file_name, $this->agora_config['o_uploadile_thumb_size']);
		//}
		$this->upload_model->makeThumb($this->agora_user['id'], $file_name, $this->agora_config['o_uploadile_disp_size'], '');
		
		if ($redir == 'self') {
			$dir = AGORA_PATH.DS.'img'.DS.'members'.DS.$this->agora_user['id'].DS;
			$web_dir = Agora::getSite().'img/members/'.$this->agora_user['id'].'/';
			// check for an override
			$cfg = Agora::config();
			if (isset($cfg['o_attachment_path']) && !empty($cfg['o_attachment_path']) && is_dir(JPATH_ROOT . DS . $cfg['o_attachment_path'])) {
				$dir = JPATH_ROOT . DS . $cfg['o_attachment_path'] . DS . $this->agora_user['id'] . DS;
				$web_dir = rtrim(JURI::root(),'/') . '/' . $cfg['o_attachment_path'] . '/' . $this->agora_user['id'] . '/';
			}
			$imgsize = getimagesize($dir.$file_name);
			
			if (isset($_SESSION['add_attachment'])) {
				unset($_SESSION['add_attachment']);
			}
			$_SESSION['add_attachment'] = array(
				'path' => $web_dir.$file_name,
				'name' => $file_name,
				'mini' => ((is_file($dir.'mini_'.$file_name) && getimagesize($dir.'mini_'.$file_name)) ? $web_dir.'mini_'.$file_name : ''),
				'width' => ($imgsize ? $imgsize[0] : ''),
				'height' => ($imgsize ? $imgsize[1] : ''),
			);
			
			Agora::redirect(Agora::getRefferer());
		} else {
			$this->redirect('type','!page','!action');
		}
	}
}
?>
